package cn.bonoon.services.plugins;

import java.util.Calendar;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bonoon.core.plugins.PersonalHolidayService;
import cn.bonoon.entities.calendars.HolidayPersonalEntity;
import cn.bonoon.kernel.support.IOperator;
import cn.bonoon.kernel.support.services.AbstractService;

@Service
@Transactional(readOnly = true)
public class PersonalHolidayServiceImpl extends AbstractService<HolidayPersonalEntity> implements PersonalHolidayService {

	@Override
	public List<HolidayPersonalEntity> getYear(IOperator opt, int year) {
		return __list(HolidayPersonalEntity.class, "from PersonalHolidayEntity x where x.creatorId=? and x.year=?", opt.getId(), year);
	}

	@Override
	public List<HolidayPersonalEntity> getMonth(IOperator opt, int year, int month) {
		return __list(HolidayPersonalEntity.class, "from PersonalHolidayEntity x where x.creatorId=? and x.year=? and x.month=?", opt.getId(), year, month);
	}

	@Override
	public List<HolidayPersonalEntity> getDate(IOperator opt, int year, int month, int date) {
		return __list(HolidayPersonalEntity.class, "from PersonalHolidayEntity x where x.creatorId=? and x.year=? and x.month=? and x.date=?", opt.getId(), year, month, date);
	}

	@Override
	public boolean hasPersonalHoliday(IOperator opt, int year, int month, int date) {
		return __exsit("select count(*) from PersonalHolidayEntity x where x.creatorId=? and x.year=? and x.month=? and x.date=?", opt.getId(), year, month, date);
	}

	@Override
	@Transactional
	public void delete(IOperator opt, Long[] ids) {
		if (null != ids && ids.length != 0) {
			for (Long id : ids) {
				HolidayPersonalEntity entity = __get(id);
				entityManager.remove(entity);
			}
		}
	}

	@Override
	public List<HolidayPersonalEntity> getWeek(IOperator opt, Calendar c_start, Calendar c_end) {
		int dates_start = c_start.get(Calendar.YEAR) * Y + c_start.get(Calendar.MONTH) * M + c_start.get(Calendar.DATE);
		int dates_end = c_end.get(Calendar.YEAR) * Y + c_end.get(Calendar.MONTH) * M + c_end.get(Calendar.DATE);
		return __list(HolidayPersonalEntity.class, "from PersonalHolidayEntity x where x.creatorId=? and x.dates>=? and x.dates<=?", opt.getId(), dates_start, dates_end);
	}

}
